home *** CD-ROM | disk | FTP | other *** search
/ Ultra Pack / UltraComputing Technology Demos and Tools.iso / java / demo / ImageMap / TickerArea.java < prev   
Encoding:
Java Source  |  1996-04-26  |  3.6 KB  |  128 lines

  1. /*
  2.  * @(#)TickerArea.java    1.2 96/04/24  
  3.  *
  4.  * Copyright (c) 1994-1996 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL or COMMERCIAL purposes and
  8.  * without fee is hereby granted. 
  9.  * Please refer to the file http://java.sun.com/copy_trademarks.html
  10.  * for further important copyright and trademark information and to
  11.  * http://java.sun.com/licensing.html for further important licensing
  12.  * information for the Java (tm) Technology.
  13.  * 
  14.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  15.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  16.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  18.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  19.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  20.  * 
  21.  * THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
  22.  * CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
  23.  * PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
  24.  * NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
  25.  * SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
  26.  * SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
  27.  * PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES").  SUN
  28.  * SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
  29.  * HIGH RISK ACTIVITIES.
  30.  */
  31.  
  32. import java.awt.Graphics;
  33. import java.awt.Color;
  34. import java.awt.Font;
  35. import java.awt.FontMetrics;
  36. import java.util.StringTokenizer;
  37.  
  38. /**
  39.  * This ImageArea renders a string of text that constantly scrolls across
  40.  * the indicated area of the ImageMap in the specified color.
  41.  *
  42.  * @author    Jim Graham
  43.  * @version    1.2, 04/24/96
  44.  */
  45. class TickerArea extends ImageMapArea {
  46.  
  47.     String tickertext;
  48.     Color  tickercolor;
  49.     Font   tickerfont;
  50.     int    speed;        // In pixels per second for scrolling
  51.  
  52.     int tickerx;
  53.     int tickery;
  54.     int tickerlen;
  55.     long lasttick;
  56.  
  57.     public void handleArg(String s) {
  58.     StringTokenizer st = new StringTokenizer(s, ",");
  59.  
  60.     tickertext = st.nextToken();
  61.     tickercolor = Color.black;
  62.     speed = 100;
  63.     String fontname = "TimesRoman";
  64.  
  65.     if (st.hasMoreTokens()) {
  66.         fontname = st.nextToken();
  67.         if (st.hasMoreTokens()) {
  68.         String str = st.nextToken();
  69.         if (str.startsWith("#")) {
  70.             str = str.substring(1);
  71.         }
  72.         try {
  73.             int colorval = Integer.parseInt(str, 16);
  74.             tickercolor = new Color((colorval >> 16) & 0xff,
  75.                         (colorval >> 8) & 0xff,
  76.                         (colorval >> 0) & 0xff);
  77.         } catch (Exception e) {
  78.             tickercolor = Color.black;
  79.         }
  80.         if (st.hasMoreTokens()) {
  81.             str = st.nextToken();
  82.             try {
  83.             speed = Integer.parseInt(str);
  84.             } catch (Exception e) {
  85.             speed = 100;
  86.             }
  87.         }
  88.         }
  89.     }
  90.  
  91.     FontMetrics fm;
  92.     int size;
  93.     int nextsize = H;
  94.     do {
  95.         size = nextsize;
  96.         tickerfont = new Font(fontname, Font.PLAIN, size);
  97.         fm = parent.getFontMetrics(tickerfont);
  98.         nextsize = (size * 9) / 10;
  99.     } while (fm.getHeight() > H && size > 0);
  100.     tickerlen = fm.stringWidth(tickertext);
  101.     tickery = fm.getAscent();
  102.     }
  103.  
  104.     public void getMedia() {
  105.     tickerx = 0;
  106.     repaint();
  107.     lasttick = System.currentTimeMillis();
  108.     }
  109.  
  110.     public boolean animate() {
  111.     long curtick = System.currentTimeMillis();
  112.     tickerx -= ((speed * (curtick - lasttick)) / 1000);
  113.     if (tickerx > W || tickerx + tickerlen < 0) {
  114.         tickerx = W;
  115.     }
  116.     repaint();
  117.     lasttick = curtick;
  118.     return true;
  119.     }
  120.  
  121.     public void highlight(Graphics g) {
  122.     g.setColor(tickercolor);
  123.     g.setFont(tickerfont);
  124.     g.drawString(tickertext, X+tickerx, Y+tickery);
  125.     }
  126. }
  127.  
  128.